﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using ThreeByte.ActiveDeck.Config;
using System.Xml.Linq;
using System.Net;
using System.Xml;
using System.ComponentModel;
using ThreeByte.ActiveDeck.Data;
using ThreeByte.ActiveDeck.Service;
using System.Threading;
using ThreeByte.ActiveDeck.Content;
using Microsoft.Office.Interop.PowerPoint;
using ThreeByte.ActiveDeck.AddIn.Config;
using ThreeByte.Media;


namespace ThreeByte.ActiveDeck.Controls
{
    /// <summary>
    /// Interaction logic for PublishControl.xaml
    /// </summary>
    public partial class PublishControl : UserControl
    {

        public event EventHandler Close;
        public CoreConfig Config;

        private UploadPresentationTask _uploadTask;
        private ElapsedTimer _elapsedTimer;

        public PublishControl(CoreConfig config) {
            InitializeComponent();
            Config = config;

            _uploadTask = new UploadPresentationTask(config, Dispatcher);
            _uploadTask.Completed += new EventHandler(_uploadTask_Completed);
            DataContext = _uploadTask;
            //Bind the important elements
            _elapsedTimer = new ElapsedTimer();
            ElapsedTimeText.DataContext = _elapsedTimer;
        }

        void _uploadTask_Completed(object sender, EventArgs e) {
            _elapsedTimer.Stop();
            if(_uploadTask.Canceled) {
                ErrorMessage = "Publishing was canceled.";
            }
            if(_uploadTask.HasError && !(_uploadTask.Canceled)) {
                DisplayUploadError(_uploadTask.Error);
            } else {
                CancelButton.IsEnabled = false;
                SubTaskGrid.Visibility = System.Windows.Visibility.Hidden;
                CancelButton.Visibility = System.Windows.Visibility.Hidden;
                DoneButton.Visibility = System.Windows.Visibility.Visible;
            }
        }


        private void CancelButton_Click(object sender, RoutedEventArgs e) {

            _uploadTask.Cancel();
        }

        public string ErrorMessage { get; private set; }

        private void DisplayUploadError(string message) {
            MessageBox.Show(message, "Upload Error");
            ErrorMessage = message;

            CancelButton.IsEnabled = false;
            PublishProgressBar.IsEnabled = false;
            SubTaskGrid.Visibility = System.Windows.Visibility.Hidden;
        }

        
        private void UserControl_Loaded(object sender, RoutedEventArgs e) {

            _elapsedTimer.Start();
            _uploadTask.Setup();
            _uploadTask.Go();

            CancelButton.IsEnabled = true;
            SubTaskGrid.Visibility = System.Windows.Visibility.Visible;
        }
  

        private void DoneButton_Click(object sender, RoutedEventArgs e) {
            if(Close != null) {
                Close(this, EventArgs.Empty);
            }
        }

        //Returns true if the instance was running when canceled, false otherwise
        internal bool SendCancel() {
            bool wasComplete = _uploadTask.Complete;
            _uploadTask.Cancel();
            return !wasComplete;
        }
    }

}
